import {mapState} from 'vuex'
import transfer from '../../../core/transfer'
import {GET_ORDERS, IGNORE_ORDER, ORDER_TAKING,VERSION} from "../../../assets/js/api"

import {orderMixins} from '../../../core/mixins'

export default {
  name: 'order',
  mixins: [orderMixins],
  data() {
    return {
      //tab标签
      filterItem: {
        0: {name: '全部', page: 1, allLoaded: false, loading: false},
        1: {name: '接单', page: 1, allLoaded: false, loading: false},
        2: {name: '待服务', page: 1, allLoaded: false, loading: false},
        3: {name: '服务中', page: 1, allLoaded: false, loading: false},
        4: {name: '已完成', page: 1, allLoaded: false, loading: false}
      },
      //当前标签索引
      currentTabIndex: 0,
      //订单对象数组
      orderList: {
        0: [], 1: [], 2: [], 3: [], 4: []
      },
    }
  },
  async created() {
    //若为登录状态
    if (this.currentUser.token) {
      let index = this.currentTabIndex;
      let jsonData = await this.getOrders({});
      this.$set(this.orderList, 0, jsonData);
      this.filterItem[0].allLoaded = jsonData.length < 10;
      this.firstLoad = false;
    }
    if ($prod) {
      const string = device.platform;
      const oldVersion = AppVersion.version;
      const json = await this.$http.get(VERSION, null, {show: true, type: 'mini'});
      console.log(string)
      if (json.result === 1) {
        const newVersion = string === 'Android' ? json.data.store_android_beta : json.data.store_ios_beta;
        const url = string === 'Android' ? json.data.store_android_url : json.data.store_ios_url;
        const content = json.data.market_content;
        console.log(url)
        if (oldVersion !== newVersion && json.data.store_update_force === 1) {
          this.$modal({message: `为了不影响您的正常使用,请下载最新版本，更新内容: ${content}`, cancel: true,lock: true})
            .then(action => {
              window.location.assign(url)
              this.$toast('正在下载中,请稍后再用');
              navigator.app.exitApp();
            },()=>{
              navigator.app.exitApp();
            })
        } else {
          // this.$toast('您当前使用的是最新版本了');
        }
      }
    }
  },
  mounted() {
    // transfer.$on('finished', (order_id, arrIndex) => {
    //   let index = this.currentTabIndex;
    //   this.orderList[index].splice(arrIndex, 1);
    //   if (this.orderList[index].length === 0) {
    //     this.filterItem[index].allLoaded = true;
    //   }
    // });
  },
  methods: {
    async getOrders({tab = this.currentTabIndex, page = 1, next = false, pull = false}) {
      // console.log(tab)
      let loadingObj = pull ? {} : {show: this.firstLoad, type: 'mini'};
      let json = await this.$http.post(GET_ORDERS, {type: tab, page}, loadingObj);
      this.filterItem[tab].page = page;
      if (json.result === 1) {
        return json.data;
      }
    },
    //忽略接单
    ignoreOrder(order_id, index) {
      this.$modal({
        title: '温馨提示',
        message: '您确定拒绝此项工作吗？',
        cancel: false
      }).then(async action => {
        let json = await this.$http.post(IGNORE_ORDER, {order_id}, {show: true, type: 'mini'});
        if (json.result === 1) {
          this.orderList[this.currentTabIndex].splice(index, 1);
          this.reComputeOrder();
          this.$toast('拒绝成功');
        }
      })
    },
    //订单按钮 2.接单 3.开始服务 4.完成 流程总控制
    orderAction(type, order_id, num, order_status) {
      let formData = {type, order_id};
      switch (type) {
        case 2:
          this.orderTaking(formData, num, order_status);
          break;
        case 3:
          this.setOut(formData, num, order_status);
          break;
        case 4:
          this.$router.push({name: 'serviceLog', params: {order_id, arrIndex: num}});
          break;
        default:
          break;
      }
    },
    //接单
    orderTaking(formData, order_status, index) {
      this.$modal({
        title: '温馨提示',
        message: '您确定承接该订单吗？',
        cancel: false
      }).then(async (action) => {
        let json = await this.$http.post(ORDER_TAKING, formData, {show: true, type: 'mini'});

        if (json.result === 1) {
          const tabIndex = this.currentTabIndex;
          switch (tabIndex) {
            case 0:
              this.orderList[0][index].order_status = 2;
              break;
            case 1:
              this.orderList[this.currentTabIndex].splice(index, 1);
              this.reComputeOrder();
              break;
          }
          this.$toast('接单成功');
        }
      });
    },
    setOut(formData, index, order_status) {
      this.$modal({
        title: '温馨提示',
        message: '您确定开始服务吗？',
        cancel: false,
        lock: true,
      }).then(async (action) => {
          let json = await this.$http.post(ORDER_TAKING, formData, {show: true, type: 'mini'});
          if (json.result === 1) {
            const tabIndex = this.currentTabIndex;
            switch (tabIndex) {
              case 0:
                this.orderList[this.currentTabIndex][index].order_status = 3;
                break;
              case 2:
                this.orderList[this.currentTabIndex].splice(index, 1);
                this.reComputeOrder();
                break;
            }
            this.$toast({message: '祝您顺利', position: 'bottom'});
            this.$router.replace({name: 'order'})
          }
        }
      );
    },
    order_status(order_status) {
      switch (order_status) {
        case 1:
          return '接单'
          break;
        case 2:
          return '待服务'
          break;
        case 3:
          return '服务中'
          break;
        case 4:
          return '已完成'
          break;
        case 5:
          return '已取消'
          break;
      }
    }
  },
  computed: {
    ...mapState([
      'currentUser'
    ]),
    // order_status(status){
    //   // if(status===5){
    //   //   return '已取消'
    //   // }
    // }
  }
}
